package com.bright.hxj.qchz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bright.ghj.common.util.StringUtil;
import com.bright.hxj.qchz.mapper.FileListNewRulesMapper;
import com.bright.hxj.qchz.mapper.SRuleOuterMapper;
import com.bright.hxj.qchz.pojo.bo.NewRules;
import com.bright.hxj.qchz.pojo.po.FileListNewRules;
import com.bright.hxj.qchz.pojo.po.SRuleOuter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class Duijie_QCHZ_GetRuleManager {

    private final FileListNewRulesMapper fileListNewRulesMapper;
    private final SRuleOuterMapper sRuleOuterMapper;

    public List<NewRules> getCheckRulesByFileName(String fileName) {

        List<NewRules> rvalue = new ArrayList<>();
//            String sql = "select tableName,[rule],description,[type],gsType,tableNameStr from filelist_newRules with(nolock) where 1=1 and isnull(isShengSkip,0)=0";
//            if (!fileName.equalsIgnoreCase("")) {
//                sql += " and tableName= '" + fileName + "'";
//            }
//            sql += " and visible=1 order by disid";
//            try {
//                List<List<Object>> data = jdbcTemplate.queryForListList(sql);
        LambdaQueryWrapper<FileListNewRules> queryWrapper = new QueryWrapper<FileListNewRules>().lambda();
        if (StringUtil.isNotBlank(fileName)) {
            queryWrapper = queryWrapper.eq(FileListNewRules::getTableName, fileName);
        }
        // and isnull(isShengSkip,0)=0 and visible=1 order by disId
        queryWrapper = queryWrapper.and(wrapper -> wrapper.isNull(FileListNewRules::getIsShengSkip).or().eq(FileListNewRules::getIsShengSkip, "0"))
                .eq(FileListNewRules::getVisible, true)
                .orderByAsc(FileListNewRules::getDisId);
        List<FileListNewRules> data = fileListNewRulesMapper.selectList(queryWrapper);


        for (FileListNewRules datasub : data) {
            NewRules newRules = new NewRules();

            newRules.setTableName(datasub.getTableName());
            newRules.setRule(datasub.getRule());
            newRules.setDescription(datasub.getDescription());
            newRules.setType(datasub.getType());
            newRules.setGsType(datasub.getGsType());
            newRules.setTableNameStr(datasub.getTableNameStr());
            rvalue.add(newRules);
        }


        return rvalue;
    }

    public List<NewRules> getNBCheckRulesByTableNameAndYears(String tableName, int years) {
        List<NewRules> rvalue = new ArrayList<>();

        // sRuleInner + sRuleOuter  暂时先查sRuleInner
//        JdbcTemplateUtil cwJdbcTemplate = getCwJdbcTemplate();
//        String sql = "select tableDec,decExpress,sourceExpress,opt,exCond,sourceCondExpress,Detail,stype,tableFlag from _sRuleOuter with(nolock) where 1=1 and years=? and tableDec=?";
        LambdaQueryWrapper<SRuleOuter> queryWrapper = new LambdaQueryWrapper<SRuleOuter>().eq(SRuleOuter::getYears, years).eq(SRuleOuter::getTableDec, tableName);
        try {
//            List<List<Object>> data = cwJdbcTemplate.queryForListList(sql, new Object[] {years, tableName});
            List<SRuleOuter> sRuleOuters = sRuleOuterMapper.selectList(queryWrapper);
            for (SRuleOuter dataSub : sRuleOuters) {
                NewRules newRules = new NewRules();

                newRules.setTableName(dataSub.getTableDec());
                newRules.setLeftRule(dataSub.getDecExpress());
                newRules.setRightRule(dataSub.getSourceExpress());
                newRules.setOpt(dataSub.getOpt());
                newRules.setExCond(dataSub.getExCond());
                newRules.setSourceCondExpress(dataSub.getSourceCondExpress());
                newRules.setDescription(dataSub.getDetail());
                newRules.setType(dataSub.getStype()); // 年报的stype
                String tableFlag = dataSub.getTableflag() == null ? "" : dataSub.getTableflag();
                newRules.setGsType(StringUtils.equals(tableFlag, "表间") ? 2 : 1);
                rvalue.add(newRules);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rvalue;
    }

//    @Deprecated
//    public List<CheckRule> getCheckRulesByFileName(String fileName) {
//        List<CheckRule> result = new ArrayList<>();
//
//        List<List<Object>> lists = duijie_shiJieShou_jdbcTemplate.queryForListList("select fileName, checkType, description, expression, keyValueMap, isCrossTable, crossTableMap from checkRules where fileName=? and visible=1 order by disid");
//        for (List<Object> list : lists) {
//            result.add(getCheckRule(list));
//        }
//
//        return result;
//    }
//
//    @Deprecated
//    private CheckRule getCheckRule(List<Object> list) {
//        CheckRule checkRule = new CheckRule();
//        checkRule.setFileName((String) list.get(0));
//        checkRule.setCheckType((int) list.get(1));
//        checkRule.setDescription((String) list.get(2));
//        checkRule.setExpression((String) list.get(3));
//        checkRule.setKeyValueMap((String) list.get(4));
//        checkRule.setCrossTable((boolean) list.get(5));
//        checkRule.setCrossTableMap((String) list.get(6));
//
//        return checkRule;
//    }


}
